package com.tm.config;

import com.github.xiaoymin.swaggerbootstrapui.annotations.EnableSwaggerBootstrapUI;
import io.swagger.annotations.ApiOperation;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

/**
 * @PROJECT_NAME: tm-wm-syb
 * @DESCRIPTION:
 * @USER: DELL
 * @DATE: 2023/4/3 16:46
 */
@Configuration
@EnableSwagger2
@EnableSwaggerBootstrapUI
public class WebMvcConfig extends WebMvcConfigurationSupport {

    /**
     * 显示swagger-ui.html文件展示页。还必须注入swagger资源
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry){
        //静态文件映射
        registry.addResourceHandler("/front/**").addResourceLocations("classpath:/front/");
        registry.addResourceHandler("/backend/**").addResourceLocations("classpath:/backend/");

        //         //映射本地目录
         registry.addResourceHandler("/img/**").addResourceLocations("file:C:\\Users\\DELL\\zhaopian\\");
         //映射classpath下路径
   //   registry.addResourceHandler("/img/**").addResourceLocations("classpath:/static/");
        registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");

    }


    /**
     * 帮助中心（不同的模块这里分不同的包扫描basePackage）
     * Dacket 可以配置多个
     */
@Bean
    public Docket assist(){
    return new Docket(DocumentationType.SWAGGER_2)
            .globalOperationParameters(setRequestHeaders())
            //.apiInfo(apiIfo())
            .select()
            .apis(RequestHandlerSelectors.basePackage("com.tm.controller"))
            //加了ApiOperotion注解的类 才生成接口文档
            .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
            .paths(PathSelectors.any())
            .build()
            .groupName("帮助中心");
}

    /**
     * 设置请求头
     * @return
     */
    private List<Parameter> setRequestHeaders() {
        ParameterBuilder ticketPar = new ParameterBuilder();
        List<Parameter> pars = new ArrayList<>();
        ticketPar.name("token").description("用户token")
                .modelRef(new ModelRef("spring")).parameterType("header")
                .required(false).build();//header中的ticket参数非必填。传空也可以
        pars.add(ticketPar.build());//根据每个方法名也知道当前方法在设置什么参数
        return pars;
    }


}
